package com.ynet.hmds28.user.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.boot.hsf.annotation.HSFProvider;
import com.taobao.pandora.boot.loader.util.StringUtils;
import com.ynet.hm.user.service.QueryServiceCallFlowService;
import com.ynet.hm.utils.config.Result;
import com.ynet.hm.utils.config.ResultUtil;
import com.ynet.hmds28.user.dao.PlatflowDao;
import com.ynet.hmds28.user.dao.UserDao;
import com.ynet.hmds28.user.model.Platflow;
import com.ynet.hmds28.user.model.PlatflowQuery;
import com.ynet.hmds28.user.model.User;
import com.ynet.hmds28.user.model.extend.PlatflowExtend;
@Transactional
@HSFProvider(serviceInterface = QueryServiceCallFlowService.class, serviceVersion = "1.0.0",serviceGroup = "hmds28")
public class QueryServiceCallFlowServiceImpl implements QueryServiceCallFlowService {
	@Autowired
	private PlatflowDao platflowDao;
	@Autowired
	private UserDao userDao;
	@Override
	public Result<?> queryServiceCallFlow(String serviceConsumeId,
			String pageNo, String pageSize) {
		PlatflowQuery example=new PlatflowQuery();
		example.createCriteria().andServiceconsumeidEqualTo(serviceConsumeId);
		int totalNum=platflowDao.countByExample(example);
		if(StringUtils.isEmpty(pageNo)){
			pageNo="1";
		}
		if(StringUtils.isEmpty(pageSize)){
			pageSize="10";
		}
		example.setOrderByClause("callTime desc");
		example.setPageNo(Integer.parseInt(pageNo));
		example.setPageSize(Integer.parseInt(pageSize));
		example.setStartRow((Integer.parseInt(pageNo)-1)*Integer.parseInt(pageSize));
		List<Platflow> pts=platflowDao.selectByExample(example);
		List<PlatflowExtend> pfes=new ArrayList<>();
		if(!CollectionUtils.isEmpty(pts)){
			for(Platflow p:pts){
				PlatflowExtend pfe=new PlatflowExtend();
				pfe.setCalltime(p.getCalltime());
				pfe.setExportmessage(p.getExportmessage());
				pfe.setImportmessage(p.getImportmessage());
				String userId=p.getConsumeuserid();
				User user=userDao.selectByPrimaryKey(userId);
				pfe.setConsumeName(user.getUsername());
				pfe.setPlatflowid(p.getPlatflowid());
				pfes.add(pfe);
			}
		}
		Map<String,Object> m=new HashMap<>();
		m.put("totalNum", totalNum);
		m.put("platflow", pfes);
		return ResultUtil.success(m);
	}

}
